﻿#include "salecalculatewidget.h"
#include "ui_salecalculatewidget.h"

SaleCalculateWidget::SaleCalculateWidget(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::SaleCalculateWidget)
{
    ui->setupUi(this);
    this->dingdan = nullptr;

    connect(&dingdanService, &DingdanService::calculateDingdanSuccess,
            [=]()
    {
        this->close();
        emit this->calculateDingdanSuccess();
    });

    setTabOrder(ui->lineEditSourceFreightPaid, ui->lineEditSourceFreightUnpaid);
    setTabOrder(ui->lineEditSourceFreightUnpaid, ui->lineEditTargetFreightPaid);
    setTabOrder(ui->lineEditTargetFreightPaid, ui->lineEditFreeTicket);
    setTabOrder(ui->lineEditFreeTicket, ui->lineEditFreeMoney);
    setTabOrder(ui->lineEditFreeMoney, ui->lineEditTicket);
    setTabOrder(ui->lineEditTicket, ui->lineEditMoney);
    setTabOrder(ui->lineEditMoney, ui->pushButtonSubmit);
    setTabOrder(ui->pushButtonSubmit, ui->pushButtonCancel);
}

SaleCalculateWidget::~SaleCalculateWidget()
{
    delete ui;
}

void SaleCalculateWidget::setTicketState(bool isTicket){
    ui->lineEditFreeTicket->setEnabled(isTicket);
    ui->lineEditTicket->setEnabled(isTicket);
}

void SaleCalculateWidget::setWidgetType(Dingdan::DingdanType widgetType){
    this->widgetType = widgetType;

    switch (widgetType) {
    case Dingdan::SaleOut:
        setWindowTitle("销售单结算");
        ui->labelFreeTicket->setText("免开票");
        ui->labelFreeMoney->setText("免收款");
        ui->labelTicket->setText("开票");
        ui->labelMoney->setText("收款");
        break;
    case Dingdan::SaleBack:
        setWindowTitle("销退单结算");
        ui->labelFreeTicket->setText("免收票");
        ui->labelFreeMoney->setText("免付款");
        ui->labelTicket->setText("收票");
        ui->labelMoney->setText("付款");
        break;
    case Dingdan::PurchaseIn:
        setWindowTitle("采购单结算");
        ui->labelFreeTicket->setText("免收票");
        ui->labelFreeMoney->setText("免付款");
        ui->labelTicket->setText("收票");
        ui->labelMoney->setText("付款");
        break;
    case Dingdan::PurchaseBack:
        setWindowTitle("采退单结算");
        ui->labelFreeTicket->setText("免开票");
        ui->labelFreeMoney->setText("免收款");
        ui->labelTicket->setText("开票");
        ui->labelMoney->setText("收款");
        break;
    case Dingdan::PurchaseDingdan:
        break;
    default:
        break;
    }
}

void SaleCalculateWidget::showWithDingdan(Dingdan *dingdan){
    this->dingdan = dingdan;

    ui->lineEditSourceFreightPaid->setText("0");
    ui->lineEditSourceFreightUnpaid->setText(QString("%1").arg(dingdan->sourceFreightUnpaid));
    ui->lineEditTargetFreightPaid->setText(QString("%1").arg(dingdan->targetFreightPaid));
    ui->lineEditFreeTicket->setText(QString("%1").arg(dingdan->freeTicket));
    ui->lineEditFreeMoney->setText(QString("%1").arg(dingdan->freeMoney));
    ui->lineEditTicket->setText(QString("%1").arg(dingdan->ticket));
    ui->lineEditMoney->setText(QString("%1").arg(dingdan->money));

    exec();
}

void SaleCalculateWidget::checkValue(QLineEdit *lineEdit){
    double value = lineEdit->text().toDouble();
    if(value < 0){
        lineEdit->setText("0");
    }
}

void SaleCalculateWidget::on_lineEditSourceFreightPaid_editingFinished()
{
    checkValue(ui->lineEditSourceFreightPaid);
}

void SaleCalculateWidget::on_lineEditSourceFreightUnpaid_editingFinished()
{
    checkValue(ui->lineEditSourceFreightUnpaid);
}

void SaleCalculateWidget::on_lineEditTargetFreightPaid_editingFinished()
{
    checkValue(ui->lineEditTargetFreightPaid);
}

void SaleCalculateWidget::on_lineEditFreeTicket_editingFinished()
{
    checkValue(ui->lineEditFreeTicket);
}

void SaleCalculateWidget::on_lineEditFreeMoney_editingFinished()
{
    checkValue(ui->lineEditFreeMoney);
}

void SaleCalculateWidget::on_lineEditTicket_editingFinished()
{
    checkValue(ui->lineEditTicket);
}

void SaleCalculateWidget::on_lineEditMoney_editingFinished()
{
    checkValue(ui->lineEditMoney);
}

void SaleCalculateWidget::on_pushButtonCancel_clicked()
{
    this->close();
}

void SaleCalculateWidget::on_pushButtonSubmit_clicked()
{
    if(dingdan == nullptr){
        return;
    }

    if(!userService.inputOperatePassword(this)){
        return;
    }

    dingdan->sourceFreightPaid = ui->lineEditSourceFreightPaid->text().toDouble();
    dingdan->sourceFreightUnpaid = ui->lineEditSourceFreightUnpaid->text().toDouble();
    dingdan->targetFreightPaid = ui->lineEditTargetFreightPaid->text().toDouble();
    dingdan->ticket = ui->lineEditTicket->text().toDouble();
    dingdan->money = ui->lineEditMoney->text().toDouble();
    dingdan->freeTicket = ui->lineEditFreeTicket->text().toDouble();
    dingdan->freeMoney = ui->lineEditFreeMoney->text().toDouble();

    dingdanService.calculateDingdan(this, ui->pushButtonSubmit, *dingdan);
}
